package com.yc.springmvc;

import com.yc.springmvc.web.MyDispatchServlet;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class WebConfig implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(MvcConfig.class);

        //设置beans.xml 路径
        servletContext.setInitParameter("contextConfigLocation","classpath:beans.xml");


        // 分发器Servlet
        DispatcherServlet servlet = new DispatcherServlet(ctx);
        //MyDispatchServlet servlet = new MyDispatchServlet();
        ServletRegistration.Dynamic dispatchServlet = servletContext
                .addServlet("dispatchServlet", servlet);
        dispatchServlet.setLoadOnStartup(1);
        dispatchServlet.addMapping("/");
        dispatchServlet.setMultipartConfig(new MultipartConfigElement("/"));
    }
}
